package com.cheyunkeji.er.adapter.auction;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.auction.BaojiaBean;
import com.cheyunkeji.er.utils.ImageLoadUtil;
import com.cheyunkeji.er.utils.TimeUtils;
import com.cheyunkeji.er.utils.countdown.CountDownTask;
import com.cheyunkeji.er.utils.countdown.CountDownTimers;
import com.cheyunkeji.er.utils.countdown.Objects;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/2/24 14:40. */
public class BaoJiaLvAdapter extends MyBaseAdapter {

  private static final String TAG = BaoJiaLvAdapter.class.getSimpleName();

  private CountDownTask mCountDownTask;

  private int baojiaType = 0;

  public BaoJiaLvAdapter(ArrayList<BaojiaBean> dataList, Context context, int baoJiaType) {
    super(dataList, context);
    this.baojiaType = baoJiaType;
  }

  public void setCountDownTask(CountDownTask countDownTask) {
    if (!Objects.equals(mCountDownTask, countDownTask)) {
      mCountDownTask = countDownTask;
      notifyDataSetChanged();
    }
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.er_lv_item_baojia, null);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    BaojiaBean itemBean = (BaojiaBean) dataList.get(position);

    if (!TextUtils.isEmpty(itemBean.getCover()))
      ImageLoadUtil.loadImg(context, itemBean.getCover(), holder.ivBaojiaItem);
    else holder.ivBaojiaItem.setImageResource(R.drawable.er_default_img_select_bkg);

    //        holder.tvCarBrand.setText(itemBean.getBrand_name() + itemBean.getSeries_name());
    //        holder.tvDesc.setText(itemBean.getChannel_name());

    holder.tvCarBrand.setText(itemBean.getTitle());
    holder.tvDesc.setText(itemBean.getIntro());

    if (holder.tvPriceOrLeftTime.getVisibility() == View.INVISIBLE) {
      holder.tvPriceOrLeftTime.setVisibility(View.VISIBLE);
    }

    switch (baojiaType) {
      case Constants.BAOJIA_TYPE_XUNJIA:
        holder.tvBaojiaState.setText("距离结束：");
        if (Long.valueOf(itemBean.getEtime()) * 1000 > System.currentTimeMillis()) {
          startCountDown(
              position,
              CountDownTask.elapsedRealtime()
                  + Long.valueOf(itemBean.getEtime()) * 1000
                  - System.currentTimeMillis(),
              holder.tvPriceOrLeftTime);
        } else {
          cancelCountDown(
              position, Long.valueOf(itemBean.getEtime()) * 1000, holder.tvPriceOrLeftTime);
        }
        break;

      case Constants.BAOJIA_TYPE_GIVE_ALREADY:
        holder.tvBaojiaState.setText("我的报价：");
        holder.tvPriceOrLeftTime.setText(itemBean.getCallprice() + "元");

        break;

      case Constants.BAOJIA_TYPE_MISSED:
        holder.tvBaojiaState.setText("已结束");
        holder.tvPriceOrLeftTime.setVisibility(View.INVISIBLE);

        break;

      default:
        break;
    }

    return convertView;
  }

  /**
   * 开始计时
   *
   * @param position item位置
   * @param leftTimeMillis 剩余时间的毫秒值
   * @param view 更新显示时间的TextView
   */
  private void startCountDown(final int position, long leftTimeMillis, TextView view) {
    if (mCountDownTask != null) {
      mCountDownTask.until(
          view,
          leftTimeMillis,
          1000,
          new CountDownTimers.OnCountDownListener() {
            @Override
            public void onTick(View view, long millisUntilFinished) {
              doOnTick(position, view, millisUntilFinished, 1000);
            }

            @Override
            public void onFinish(View view) {
              doOnFinish(position, view);
            }
          });
    }
  }

  /**
   * 取消计时
   *
   * @param position item位置
   * @param leftTimeMillis 剩余时间毫秒值
   * @param view 更新显示时间的View
   */
  private void cancelCountDown(final int position, long leftTimeMillis, TextView view) {
    if (mCountDownTask != null) {
      mCountDownTask.cancel(view);
    }
  }

  private void doOnTick(int position, View view, long millisUntilFinished, int intervel) {
    ((TextView) view).setText(TimeUtils.leftMillis2String(millisUntilFinished));
  }

  private void doOnFinish(int position, View view) {
    ((TextView) view).setText("结束");
    //        view.setVisibility(View.INVISIBLE);
  }

  static class ViewHolder {
    @BindView(R.id.iv_baojia_item)
    ImageView ivBaojiaItem;

    @BindView(R.id.iv_tag)
    ImageView ivTag;

    @BindView(R.id.tv_car_brand)
    TextView tvCarBrand;

    @BindView(R.id.tv_desc)
    TextView tvDesc;

    @BindView(R.id.tv_baojia_state)
    TextView tvBaojiaState;

    @BindView(R.id.tv_price_or_left_time)
    TextView tvPriceOrLeftTime;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
